#import "mxml.h"
#import "tool_mxml.h"
#import "file_config_menu.h"

/*layer_menu_main
*/
UIImageObject g_CfgMenuMainImageBackground;
UIImageObject g_CfgMenuMainImageTitle;

CGPoint g_CfgMenuMainHeart1Position;
CGPoint g_CfgMenuMainHeart2Position;
CGPoint g_CfgMenuMainHeart3Position;

UIImageObject g_CfgMenuMainImageCloud1;
UIImageObject g_CfgMenuMainImageCloud2;
UIImageObject g_CfgMenuMainImageCloud3;

char g_CfgMenuMainImageCloudWhitePath[STRING_BUF_SIZE];
char g_CfgMenuMainImageCloudBluePath[STRING_BUF_SIZE];

UIMenuItemImageObject g_CfgMenuMainItemPlay;
UIMenuItemImageObject g_CfgMenuMainItemLoveLibrary;
UIMenuItemImageObject g_CfgMenuMainItemSurvival;
UIMenuItemImageObject g_CfgMenuMainItemFunction;
UIMenuItemImageObject g_CfgMenuMainItemOption;
UIMenuItemImageObject g_CfgMenuMainItemGameCenter;
UIMenuItemImageObject g_CfgMenuMainItemMoreGames;

UIAnimationObject g_CfgMenuMainAniFunction;
UIAnimationObject g_CfgMenuMainAniHeart;

/*layer_menu_story_chapter
*/
UIImageObject g_CfgMenuChapterImageBackground;
UIImageObject g_CfgMenuChapterImageCloud1;
UIImageObject g_CfgMenuChapterImageCloud2;
UIMenuItemImageObject g_CfgMenuChapterItemBack;
char g_CfgMenuChapterPageBack[STRING_BUF_SIZE];
char g_CfgMenuChapterPageFlower[STRING_BUF_SIZE];
char g_CfgMenuChapterStarBackground[STRING_BUF_SIZE];
char g_CfgMenuChapterStarOn[STRING_BUF_SIZE];
char g_CfgMenuChapterStarOff[STRING_BUF_SIZE];
CGPoint g_CfgMenuChapterLabelTitilePosition;
CGPoint g_CfgMenuChapterStarBackgroundPosition;
CGPoint g_CfgMenuChapterStar1Position;
CGPoint g_CfgMenuChapterStar2Position;
CGPoint g_CfgMenuChapterStar3Position;
CGPoint g_CfgMenuChapterLabelReadPosition;
CGPoint g_CfgMenuChapterLabelClosePosition;
std::vector<SChapterBook> g_CfgMenuChapterBookTable;
UIAnimationObject g_CfgMenuChapterBookOpenNextAni;

/*option
*/
UIImageObject g_CfgMenuOptionImageBackground;
UIImageObject g_CfgMenuOptionImageBgm;
UIImageObject g_CfgMenuOptionImageSoundex;
CGPoint g_CfgMenuOptionItemBgmPosition;
CGPoint g_CfgMenuOptionItemSoundexPosition;
char g_CfgMenuOptionOnPath[STRING_BUF_SIZE];
char g_CfgMenuOptionOffPath[STRING_BUF_SIZE];
UIMenuItemImageObject g_CfgMenuOptionItemBack;

void CfgMenuLoadChapterBook(mxml_node_t *Bouy, SChapterBook *Book);

void CfgMenuLoadConfigFile(void)
{
	char _Buffer[256];
	FILE *_ConfigFile;
	mxml_node_t *_NodeXml, *_NodeBouy;
	SChapterBook _Book;

	memset(_Buffer, 0, sizeof _Buffer);
	[[CCFileUtils fullPathFromRelativePath:@"file_config_menu.xml"] getCString:_Buffer maxLength:sizeof _Buffer encoding:NSASCIIStringEncoding];
	_ConfigFile = fopen(_Buffer, "r");
	if (NULL != _ConfigFile)
	{
		_NodeXml = mxmlLoadFile(NULL, _ConfigFile, MXML_TEXT_CALLBACK);
		_NodeBouy = mxmlGetFirstChild(_NodeXml);
		for (; NULL != _NodeBouy; _NodeBouy = mxmlWalkNext(_NodeBouy, _NodeXml, MXML_NO_DESCEND))
		{
			/*menu main
			*/
			if (tmxmlIsElement(_NodeBouy, "main_image_background"))
			{
				uiLoadImageObjectFromXML(_NodeBouy, &g_CfgMenuMainImageBackground);
			}
			else if (tmxmlIsElement(_NodeBouy, "main_image_title"))
			{
				uiLoadImageObjectFromXML(_NodeBouy, &g_CfgMenuMainImageTitle);
			}
			else if (tmxmlIsElement(_NodeBouy, "main_heart_1_position"))
			{
				uiLoadPointFromXML(_NodeBouy, &g_CfgMenuMainHeart1Position);
			}
			else if (tmxmlIsElement(_NodeBouy, "main_heart_2_position"))
			{
				uiLoadPointFromXML(_NodeBouy, &g_CfgMenuMainHeart2Position);
			}
			else if (tmxmlIsElement(_NodeBouy, "main_heart_3_position"))
			{
				uiLoadPointFromXML(_NodeBouy, &g_CfgMenuMainHeart3Position);
			}
			else if (tmxmlIsElement(_NodeBouy, "main_image_cloud_1"))
			{
				uiLoadImageObjectFromXML(_NodeBouy, &g_CfgMenuMainImageCloud1);
			}
			else if (tmxmlIsElement(_NodeBouy, "main_image_cloud_2"))
			{
				uiLoadImageObjectFromXML(_NodeBouy, &g_CfgMenuMainImageCloud2);
			}
			else if (tmxmlIsElement(_NodeBouy, "main_image_cloud_3"))
			{
				uiLoadImageObjectFromXML(_NodeBouy, &g_CfgMenuMainImageCloud3);
			}
			else if (tmxmlIsElement(_NodeBouy, "main_image_cloud_white_path"))
			{
				tmxmlElementReadChildText(_NodeBouy, g_CfgMenuMainImageCloudWhitePath);
			}
			else if (tmxmlIsElement(_NodeBouy, "main_image_cloud_blue_path"))
			{
				tmxmlElementReadChildText(_NodeBouy, g_CfgMenuMainImageCloudBluePath);
			}
			else if (tmxmlIsElement(_NodeBouy, "main_item_play"))
			{
				uiLoadMenuItemImageObjectFromXML(_NodeBouy, &g_CfgMenuMainItemPlay);
			}
			else if (tmxmlIsElement(_NodeBouy, "main_item_love_library"))
			{
				uiLoadMenuItemImageObjectFromXML(_NodeBouy, &g_CfgMenuMainItemLoveLibrary);
			}
			else if (tmxmlIsElement(_NodeBouy, "main_item_survival"))
			{
				uiLoadMenuItemImageObjectFromXML(_NodeBouy, &g_CfgMenuMainItemSurvival);
			}
			else if (tmxmlIsElement(_NodeBouy, "main_item_function"))
			{
				uiLoadMenuItemImageObjectFromXML(_NodeBouy, &g_CfgMenuMainItemFunction);
			}
			else if (tmxmlIsElement(_NodeBouy, "main_item_option"))
			{
				uiLoadMenuItemImageObjectFromXML(_NodeBouy, &g_CfgMenuMainItemOption);
			}
			else if (tmxmlIsElement(_NodeBouy, "main_item_game_center"))
			{
				uiLoadMenuItemImageObjectFromXML(_NodeBouy, &g_CfgMenuMainItemGameCenter);
			}
			else if (tmxmlIsElement(_NodeBouy, "main_item_more_games"))
			{
				uiLoadMenuItemImageObjectFromXML(_NodeBouy, &g_CfgMenuMainItemMoreGames);
			}
			else if (tmxmlIsElement(_NodeBouy, "main_ani_function"))
			{
				uiLoadAnimationObjectFromXML(_NodeBouy, &g_CfgMenuMainAniFunction);
			}
			else if (tmxmlIsElement(_NodeBouy, "main_ani_heart"))
			{
				uiLoadAnimationObjectFromXML(_NodeBouy, &g_CfgMenuMainAniHeart);
			}
			/*menu chapter
			*/
			else if (tmxmlIsElement(_NodeBouy, "story_chapter_image_background"))
			{
				uiLoadImageObjectFromXML(_NodeBouy, &g_CfgMenuChapterImageBackground);
			}
			else if (tmxmlIsElement(_NodeBouy, "story_chapter_image_cloud_1"))
			{
				uiLoadImageObjectFromXML(_NodeBouy, &g_CfgMenuChapterImageCloud1);
			}
			else if (tmxmlIsElement(_NodeBouy, "story_chapter_image_cloud_2"))
			{
				uiLoadImageObjectFromXML(_NodeBouy, &g_CfgMenuChapterImageCloud2);
			}
			else if (tmxmlIsElement(_NodeBouy, "story_chapter_item_image_back"))
			{
				uiLoadMenuItemImageObjectFromXML(_NodeBouy, &g_CfgMenuChapterItemBack);
			}
			else if (tmxmlIsElement(_NodeBouy, "story_chapter_page_back"))
			{
				tmxmlElementReadChildText(_NodeBouy, g_CfgMenuChapterPageBack);
			}
			else if (tmxmlIsElement(_NodeBouy, "story_chapter_page_flower"))
			{
				tmxmlElementReadChildText(_NodeBouy, g_CfgMenuChapterPageFlower);
			}
			else if (tmxmlIsElement(_NodeBouy, "story_chapter_star_background"))
			{
				tmxmlElementReadChildText(_NodeBouy, g_CfgMenuChapterStarBackground);
			}
			else if (tmxmlIsElement(_NodeBouy, "story_chapter_star_on"))
			{
				tmxmlElementReadChildText(_NodeBouy, g_CfgMenuChapterStarOn);
			}
			else if (tmxmlIsElement(_NodeBouy, "story_chapter_star_off"))
			{
				tmxmlElementReadChildText(_NodeBouy, g_CfgMenuChapterStarOff);
			}
			else if (tmxmlIsElement(_NodeBouy, "story_chapter_label_titile_position"))
			{
				uiLoadPointFromXML(_NodeBouy, &g_CfgMenuChapterLabelTitilePosition);
			}
			else if (tmxmlIsElement(_NodeBouy, "story_chapter_background_position"))
			{
				uiLoadPointFromXML(_NodeBouy, &g_CfgMenuChapterStarBackgroundPosition);
			}
			else if (tmxmlIsElement(_NodeBouy, "story_chapter_1_position"))
			{
				uiLoadPointFromXML(_NodeBouy, &g_CfgMenuChapterStar1Position);
			}
			else if (tmxmlIsElement(_NodeBouy, "story_chapter_2_position"))
			{
				uiLoadPointFromXML(_NodeBouy, &g_CfgMenuChapterStar2Position);
			}
			else if (tmxmlIsElement(_NodeBouy, "story_chapter_3_position"))
			{
				uiLoadPointFromXML(_NodeBouy, &g_CfgMenuChapterStar3Position);
			}
			else if (tmxmlIsElement(_NodeBouy, "story_chapter_label_read_position"))
			{
				uiLoadPointFromXML(_NodeBouy, &g_CfgMenuChapterLabelReadPosition);
			}
			else if (tmxmlIsElement(_NodeBouy, "story_chapter_label_close_position"))
			{
				uiLoadPointFromXML(_NodeBouy, &g_CfgMenuChapterLabelClosePosition);
			}
			else if (tmxmlIsElement(_NodeBouy, "story_chapter_book"))
			{
				/*clear book
				*/
				memset(&_Book.center_rect, 0, sizeof(UICenterRect));
				memset(_Book.image_path, 0, sizeof _Book.image_path);
				uiClearAnimationObject(&_Book.ani_open);
				_Book.missions.clear();
				_Book.titile.clear();

				/*load book
				*/
				CfgMenuLoadChapterBook(_NodeBouy, &_Book);
				g_CfgMenuChapterBookTable.push_back(_Book);
			}
			else if (tmxmlIsElement(_NodeBouy, "story_chapter_book_open_next_ani"))
			{
				uiLoadAnimationObjectFromXML(_NodeBouy, &g_CfgMenuChapterBookOpenNextAni);
			}
			/*option
			*/
			else if (tmxmlIsElement(_NodeBouy, "option_image_background"))
			{
				uiLoadImageObjectFromXML(_NodeBouy, &g_CfgMenuOptionImageBackground);
			}
			else if (tmxmlIsElement(_NodeBouy, "option_image_option_bgm"))
			{
				uiLoadImageObjectFromXML(_NodeBouy, &g_CfgMenuOptionImageBgm);
			}
			else if (tmxmlIsElement(_NodeBouy, "option_image_option_soundex"))
			{
				uiLoadImageObjectFromXML(_NodeBouy, &g_CfgMenuOptionImageSoundex);
			}
			else if (tmxmlIsElement(_NodeBouy, "option_item_bgm_position"))
			{
				uiLoadPointFromXML(_NodeBouy, &g_CfgMenuOptionItemBgmPosition);
			}
			else if (tmxmlIsElement(_NodeBouy, "option_item_soundex_position"))
			{
				uiLoadPointFromXML(_NodeBouy, &g_CfgMenuOptionItemSoundexPosition);
			}
			else if (tmxmlIsElement(_NodeBouy, "option_image_option_on_path"))
			{
				tmxmlElementReadChildText(_NodeBouy, g_CfgMenuOptionOnPath);
			}
			else if (tmxmlIsElement(_NodeBouy, "option_image_option_off_path"))
			{
				tmxmlElementReadChildText(_NodeBouy, g_CfgMenuOptionOffPath);
			}
			else if (tmxmlIsElement(_NodeBouy, "option_item_back"))
			{
				uiLoadMenuItemImageObjectFromXML(_NodeBouy, &g_CfgMenuOptionItemBack);
			}
		}

		mxmlDelete(_NodeXml);
		fclose(_ConfigFile);
	}
	else
	{
		exit(1);
	}
}

void CfgMenuLoadChapterBook(mxml_node_t *Bouy, SChapterBook *Book)
{
	mxml_node_t *_Bouy;

	_Bouy = mxmlGetFirstChild(Bouy);
	for (; NULL != _Bouy; _Bouy = mxmlWalkNext(_Bouy, Bouy, MXML_NO_DESCEND))
	{
		if (tmxmlIsElement(_Bouy, "center_rect"))
		{
			uiLoadCenterRectFromXML(_Bouy, &Book->center_rect);
		}
		else if (tmxmlIsElement(_Bouy, "image_path"))
		{
			tmxmlElementReadChildText(_Bouy, Book->image_path);
		}
		else if (tmxmlIsElement(_Bouy, "ani_open"))
		{
			uiLoadAnimationObjectFromXML(_Bouy, &Book->ani_open);
		}
		else if (tmxmlIsElement(_Bouy, "missions"))
		{
			tmxmlReadIntTable(_Bouy, &Book->missions, "data");
		}
		else if (tmxmlIsElement(_Bouy, "titile"))
		{
			tmxmlReadStringTable(_Bouy, &Book->titile, "data");
		}
	}
}